package wjh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import wjh.pojo.Patient;
import wjh.service.wyPatientService;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/patientList")
public class wyPatientController extends HttpServlet {
    @Autowired
    private wyPatientService patientService;
    @Qualifier("httpServletRequest")
    @Autowired
    private ServletRequest httpServletRequest;

    @RequestMapping("/patientFindAll")
    public String patientFindAll(

            @RequestParam(required = false,defaultValue ="1")Integer pageNum,
            @RequestParam(required = false,defaultValue ="3")Integer pageSize,
            String likename, Model model
    ){
        PageHelper.startPage(pageNum,pageSize);
        List<Patient> patientList=patientService.like(likename);
        PageInfo<Patient> pageInfo = new PageInfo<>(patientList);
        model.addAttribute("page",pageInfo);
        model.addAttribute("likename",likename);
        return "patientList";
    }
    @RequestMapping("/addPatient")
    public String addPatient(Patient patient, MultipartFile file,HttpServletRequest request) {
        patientService.addPatient(patient,file,request);
        return "redirect:/patientList/patientFindAll.do";


    }

@RequestMapping("/patientDel")
public ResponseEntity<String> deleteDoctorByid(@RequestParam String ids) {
    try {
        patientService.deletePatientByid(ids);
        return ResponseEntity.ok("删除成功");
    } catch (Exception e) {
        // 记录异常并返回错误信息
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("删除失败");
    }
}


    @RequestMapping("/findPatientById")
    public String findPatientById(@RequestParam("id") String id, Model model) {
        Patient patient=patientService.findPatientById (Integer.parseInt(id));
        model.addAttribute("patient",patient);
        return "updatePatient";
    }
    @RequestMapping("/updatePatient")
    public String updateDoctor(Patient patient,Model model,MultipartFile file,HttpServletRequest request) {
        patientService.updatePatient(patient,file,request);
        int id = patient.getId();
        model.addAttribute("id", id);
        model.addAttribute("patient", patient);
        return "redirect:/patientList/patientFindAll.do";
    }
}
